package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.User;
import com.easy.mapper.UserMapper;
import com.easy.service.UserService;
import com.easy.util.Page;
import com.easy.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 新增用户方法（被@LogMethod标记，会被AOP拦截记录日志）
     * serviceName = "用户信息处理"：日志中标记服务名称
     * recordReturn = true：强制记录返回值（覆盖注解默认值）
     */
    @Override
    @LogMethod(serviceName = "新增用户信息",recordReturn = true)
    public User add(User user) {

        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        //新增数据
        userMapper.add(user);
        //通过主键查询数据库中的数据
        User result = userMapper.one(user.getId());
        return result;
    }


    @Override
    @LogMethod(serviceName = "删除用户信息")
    public int del(Integer id) {
        Integer result = userMapper.del(id);
        return result;
    }

    @Override
    @LogMethod(serviceName = "修改用户信息")
    public User edit(User user) {
        user.setPassword(PasswordUtil.getEncryption(user.getPassword()));
        userMapper.edit(user);
        //通过主键修改数据库中的数据
        User result = userMapper.one(user.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "修改用户密码")
    public User editPassword(User user) {
        user.setPassword(PasswordUtil.getEncryption(user.getNewPassword()));
        userMapper.editPassword(user);
        User result = userMapper.one(user.getId());
        return result;
    }


    @Override
    public List<User> all(User user) {
        return userMapper.all(user);
    }

    @Override
    public User one(Integer id) {
        return userMapper.one(id);
    }

    @Override
    public void page(Page page, User user) {
        int count = userMapper.pageCount(user);
        Object data = userMapper.page(page,user);
        page.setCount(count);
        page.setData(data);
    }
}
